'''
Created on 5. tra. 2011.

@author: Ana Banana
'''

from loading.xmlparser.mi_tab_network.Network import *;
from loading.xmlparser.mi_tab_network.Edge import *;
from loading.xmlparser.mi_tab_network.Node import *;
import re;

def parse_mitab_xml (file_name):
    mi_tab = open (file_name, 'r');
    network = Network('celeg');
    log_file = open ('log.txt', 'w');
    lines = mi_tab.readlines();
    i = -1;samci = {};
    for line in lines:
        if i == -1:
            i+=1;
            continue; #prva linija
        #prva runda - i = 0; id cvora
        rgx = re.findall(r'DIP-(.*?)(?:\|refseq:(.*?)){0,1}(?:\|uniprotkb:(.*?)){0,1}\tDIP-(.*?)(?:\|refseq:(.*?)){0,1}(?:\|uniprotkb:(.*?)){0,1}\t', line);#'(DIP-.*?)(\|refseq:(.*?))*(\|uniprotkb:(.*?))*', line);
        if (rgx):
            (dip1, ref1, unip1, dip2, ref2, unip2) = rgx[0];
            if ((ref1 == '' and unip1 == '') or (ref2 == '' and unip2 == '')):
                if (ref1 == '' and unip1 == ''):
                    samci[dip1] = ref1;
                if (ref2 == '' and unip2 == ''):
                    samci[dip2] = ref2;
                continue;
        
            n1 = Node(i, dip1, ref1, unip1);
            if (network.add_node(n1)):
                log_file.write ('Adding node ' + n1.get_dip_id() + ' , id = ' + str(n1.get_id()) + '\n');
                i+=1;
            else:
                n1 = network.get_nodes()[n1.get_dip_id()];
                log_file.write( 'Not adding node ' + n1.get_dip_id() + ', id already exists, id = ' + str(n1.get_id()) + '\n');

            n2 = Node (i, dip2, ref2, unip2);
            if (network.add_node(n2)):
                i+=1;
                log_file.write ('Adding node ' + n2.get_dip_id() + ' , id = ' + str(n2.get_id()) + '\n');
            else:
                n2 = network.get_nodes()[n2.get_dip_id()];
                log_file.write( 'Not adding node ' + n2.get_dip_id() + ', id already exists, id = ' + str(n2.get_id()) + '\n');

            if (network.get_nodes().has_key(dip1) and network.get_nodes().has_key(dip2) and dip1 != dip2):
                e = Edge (n1.get_id(), n2.get_id())
                log_file. write ( 'ADDING EDGE - ' + n1.get_dip_id() + ' AND ' + n2.get_dip_id() + '\n\n');
                network.add_edge(e)
            
        else:
            print "!!!!!!!!", line
               
    
    print 'nevaljali ', len(samci.keys());
    print 'valjani ', i
    mi_tab.close();
    log_file.close();
    return network;

def generate_gml_file (gml_file, network):
    gml = open (gml_file, 'w');
    gml.write('graph [\n');
    for node in network.get_sorted_nodes():
        gml.write ('\tnode [\n');
        gml.write ('\t\tid %d\n' % (node.get_id())) 
        gml.write ('\t\tdip_id \"DIP-%s\"\n' % node.get_dip_id())
        gml.write ('\t\trefseq \"%s\"\n' % node.get_refseq());
        gml.write ('\t\tuniprot \"%s\"\n' % node.get_uniprot_id())
        gml.write ('\t]\n');
    
    for edge in network.get_edges():
        gml.write ('\tedge [\n');
        gml.write ('\t\tsource %d\n' % edge.get_id1());
        gml.write ('\t\ttarget %d\n' % edge.get_id2());
        gml.write ('\t]\n')
    gml.write(']');

    
    

net = parse_mitab_xml(r'E:\workspace\Python\BioInfProject\MI_tab_resources\Scere20101010.xml')
#for n in net.get_sorted_nodes():
#    print n.get_id()
print len(net.get_edges())
generate_gml_file(r'E:\workspace\Python\BioInfProject\gml_networks\Scere.gml', net);